<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="G1hRwH33Kv">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.14.0/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dockerlin666.gitee.com","root":"/","scheme":"Muse","version":"8.0.0-rc.5","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false};
  </script>

  <meta name="description" content="介绍Shell 教程，主要基于bash基本等同于shShell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。Ken Thompson 的 sh 是第一种 Unix Shell，Windows Explorer 是一个典型的图形界面">
<meta property="og:type" content="article">
<meta property="og:title" content="shell">
<meta property="og:url" content="http://dockerlin666.gitee.com/2020/09/08/shell/index.html">
<meta property="og:site_name" content="阳光灿烂的日志">
<meta property="og:description" content="介绍Shell 教程，主要基于bash基本等同于shShell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。Ken Thompson 的 sh 是第一种 Unix Shell，Windows Explorer 是一个典型的图形界面">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-08T07:07:04.000Z">
<meta property="article:modified_time" content="2020-09-09T06:13:48.368Z">
<meta property="article:author" content="刀客林">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://dockerlin666.gitee.com/2020/09/08/shell/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>shell | 阳光灿烂的日志</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f59280177ba9fcb78a3c938de71308e3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">
      <img class="custom-logo-image" src="/uploads/logo400.png" alt="阳光灿烂的日志">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">阳光灿烂的日志</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">oh,my god</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">2.</span> <span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95%E7%AE%80%E4%BB%8B"><span class="nav-number">3.</span> <span class="nav-text">语法简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%98%E9%87%8F"><span class="nav-number">3.1.</span> <span class="nav-text">变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="nav-number">3.2.</span> <span class="nav-text">传递参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.3.</span> <span class="nav-text">运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E5%AE%9E%E4%BE%8B"><span class="nav-number">3.4.</span> <span class="nav-text">命令实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%85%A5-%E8%BE%93%E5%87%BA%E9%87%8D%E5%AE%9A%E5%90%91"><span class="nav-number">3.5.</span> <span class="nav-text">输入&#x2F;输出重定向</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E5%8C%85%E5%90%AB"><span class="nav-number">3.6.</span> <span class="nav-text">文件包含</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#linux-%E5%91%BD%E4%BB%A4"><span class="nav-number">3.7.</span> <span class="nav-text">linux 命令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#awk"><span class="nav-number">3.7.1.</span> <span class="nav-text">awk</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#curl"><span class="nav-number">3.7.2.</span> <span class="nav-text">curl</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#cut"><span class="nav-number">3.7.3.</span> <span class="nav-text">cut</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sed"><span class="nav-number">3.7.4.</span> <span class="nav-text">sed</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tr"><span class="nav-number">3.7.5.</span> <span class="nav-text">tr</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#uname"><span class="nav-number">3.7.6.</span> <span class="nav-text">uname</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#mktemp"><span class="nav-number">3.7.7.</span> <span class="nav-text">mktemp</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jq"><span class="nav-number">3.7.8.</span> <span class="nav-text">jq</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#xargs"><span class="nav-number">3.7.9.</span> <span class="nav-text">xargs</span></a></li></ol></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="刀客林"
      src="/uploads/avatar.jpg">
  <p class="site-author-name" itemprop="name">刀客林</p>
  <div class="site-description" itemprop="description">记录无聊的生活，要不然都忘记了！</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dockerlin666.gitee.com/2020/09/08/shell/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/avatar.jpg">
      <meta itemprop="name" content="刀客林">
      <meta itemprop="description" content="记录无聊的生活，要不然都忘记了！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="阳光灿烂的日志">
    </span>

    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          shell
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-09-08 15:07:04" itemprop="dateCreated datePublished" datetime="2020-09-08T15:07:04+08:00">2020-09-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-09 14:13:48" itemprop="dateModified" datetime="2020-09-09T14:13:48+08:00">2020-09-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%AD%A6%E4%B9%A0/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2020/09/08/shell/#SOHUCS" itemprop="discussionUrl">
        <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2020/09/08/shell/" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>13k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>12 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><div class="note info"><p><strong>Shell 教程，主要基于bash基本等同于sh</strong><br>Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。<br>Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。<br>Ken Thompson 的 sh 是第一种 Unix Shell，Windows Explorer 是一个典型的图形界面 Shell。<br>本文基本上是个简要教程，复杂的请参阅网上教程。<br><a target="_blank" rel="noopener" href="https://www.runoob.com/linux/linux-shell-basic-operators.html"><span style="color:red;">简单手册</span></a> <a target="_blank" rel="noopener" href="https://github.com/qinjx/30min_guides/blob/master/shell.md"><span style="color:red;">Shell脚本编程30分钟入门</span></a></p>
</div>

<a id="more"></a>

<h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;bash        指定脚本解释器，这里是用&#x2F;bin&#x2F;bash做解释器的</span><br><span class="line">echo &quot;hello world of shell&quot;  # 打印文本</span><br></pre></td></tr></table></figure>

<h2 id="语法简介"><a href="#语法简介" class="headerlink" title="语法简介"></a>语法简介</h2><h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"># 单行注释</span><br><span class="line">:&lt;&lt;EOF</span><br><span class="line">这是多行注释</span><br><span class="line">可以多行</span><br><span class="line">不过我看采用的人不多</span><br><span class="line">EOF</span><br><span class="line">myvar&#x3D;&quot;test&quot;                                             # 定义变量</span><br><span class="line">readonly myvar                                           # 设置变量可读</span><br><span class="line">echo &quot;myvar is $&#123;myvar&#125;&quot;                                 # 打印变量</span><br><span class="line">unset myvar                                              # unset 命令不能删除只读变量。</span><br><span class="line">echo &quot;myvar is $&#123;myvar&#125;&quot;                                 # 打印变量，变量还和原来一样</span><br><span class="line">your_name&#x3D;&#39;cxl&#39;                                          # 单引号原样</span><br><span class="line">str&#x3D;&quot;Hello, I know you are \&quot;$your_name\&quot;! \n&quot;           # 双引号里面允许变量，可以转义</span><br><span class="line">echo -e $str                                             # 打印变量，-e 启用反斜杠转义的解释</span><br><span class="line">echo &#39;hello&#39;$&#123;your_name&#125;&#39; !&#39;                             # 变量可以加大括号确认范围</span><br><span class="line">echo $&#123;#your_name&#125;                                       # 打印字符串长度</span><br><span class="line">echo $&#123;your_name:1:2&#125;                                    # 截取部分字符串</span><br><span class="line">arr&#x3D;(1 2 3 4 5)                                          # 数组声明</span><br><span class="line">echo $&#123;arr[0]&#125;                                           # 根据下标打印数组</span><br><span class="line">echo $&#123;arr[@]&#125;                                           # @符号打印所有数组元素</span><br><span class="line">length&#x3D;$&#123;#arr[@]&#125;                                        # 取得数组长度</span><br><span class="line">echo $&#123;length&#125;                                           # 打印数组长度</span><br><span class="line">for ((i&#x3D;0;i&lt;$length;i++)); do                            # 遍历打印数组        </span><br><span class="line">    echo &quot;数组$i值:$&#123;arr[$i]&#125;&quot;</span><br><span class="line">done</span><br><span class="line">for var in $&#123;arr[@]&#125;; do                                 # 遍历数组</span><br><span class="line">    echo &quot;数组值：$var&quot;</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h3 id="传递参数"><a href="#传递参数" class="headerlink" title="传递参数"></a>传递参数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;bash</span><br><span class="line"># author:菜鸟教程</span><br><span class="line"># url:www.runoob.com</span><br><span class="line"></span><br><span class="line">echo &quot;Shell 传递参数实例！&quot;;</span><br><span class="line">echo &quot;参数总数：$#&quot;</span><br><span class="line">echo &quot;当前进程id号:$$&quot;</span><br><span class="line">echo &quot;所有参数，相当于多个字符串:$@&quot;</span><br><span class="line">echo &quot;所有参数,相当于一个字符串:$*&quot;</span><br><span class="line">echo &quot;执行的文件名：$0&quot;;</span><br><span class="line">echo &quot;第一个参数为：$1&quot;;</span><br><span class="line">echo &quot;第二个参数为：$2&quot;;</span><br><span class="line">echo &quot;第三个参数为：$3&quot;;</span><br><span class="line"># 和$@相同，但&quot;$*&quot; 和 &quot;$@&quot;(加引号)并不同，&quot;$*&quot;将所有的参数解释成一个字符串，而&quot;$@&quot;是一个参数数组</span><br><span class="line"></span><br><span class="line">.&#x2F;helloshell.sh 1 2 3</span><br><span class="line">Shell 传递参数实例！</span><br><span class="line">参数总数：3</span><br><span class="line">当前进程id号:1633</span><br><span class="line">所有参数，相当于多个字符串:1 2 3</span><br><span class="line">所有参数，相当于一个字符串:1 2 3</span><br><span class="line">执行的文件名：.&#x2F;helloshell.sh</span><br><span class="line">第一个参数为：1</span><br><span class="line">第二个参数为：2</span><br><span class="line">第三个参数为：3</span><br><span class="line"></span><br><span class="line"># getopts</span><br><span class="line">#!&#x2F;bin&#x2F;bash</span><br><span class="line">while getopts &quot;a:bc&quot; arg #选项后面的冒号表示该选项需要参数</span><br><span class="line">do</span><br><span class="line">        case $arg in</span><br><span class="line">             a)</span><br><span class="line">                echo &quot;a&#39;s arg:$optarg&quot; #参数存在$optarg中</span><br><span class="line"></span><br><span class="line">             b)</span><br><span class="line">                echo &quot;b&quot;</span><br><span class="line"></span><br><span class="line">             c)</span><br><span class="line">                echo &quot;c&quot;</span><br><span class="line"></span><br><span class="line">             ?)  #当有不认识的选项的时候arg为?</span><br><span class="line">            echo &quot;unkonw argument&quot;</span><br><span class="line">        exit 1</span><br><span class="line"></span><br><span class="line">        esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"># getopt</span><br><span class="line"></span><br><span class="line">要点:</span><br><span class="line">1. getopt 用法</span><br><span class="line">语法：getopt [options] [--] optstring parameters</span><br><span class="line">例如：getopt ab:cd -a -b he free cat</span><br><span class="line">输出：-a -b he -- free cat</span><br><span class="line">            getopt 根据 ab:cd 将选项和参数 -a -b he free cat  解析为如下格式：</span><br><span class="line">            -a -b he -- free cat</span><br><span class="line">             其中 -- 将选项与非选项参数分开 free 和 cat 就时非选项参数</span><br><span class="line">2. set --</span><br><span class="line">-- Do not change any of the flags; useful in setting $1 to -.</span><br><span class="line">set --  主要是影响特殊变量$1 $2 等，其实在上面的脚本中就是将$1 $2 等参数变量重新组合</span><br><span class="line">例如：</span><br><span class="line">set -- a b c</span><br><span class="line">shell中的特殊位置变量$1 为a $2 为 b $3 为 c</span><br><span class="line">3.如上脚本为build.sh 用法如下：</span><br><span class="line">.&#x2F;build.sh -a -c -d -e dog</span><br><span class="line">.&#x2F;build.sh -acde  dog</span><br><span class="line">上面两个命令执行结果相同。</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">val&#x3D;&#96;expr 2 + 2&#96;                    # 2+2不对中间必须有空格，写成2 + 2</span><br><span class="line">echo &quot;2+2&#x3D;$val&quot;</span><br><span class="line">val&#x3D;&#96;expr 2 &#x2F; 2&#96;</span><br><span class="line">echo &quot;2&#x2F;2&#x3D;$val&quot;</span><br><span class="line">val&#x3D;&#96;expr 2 - 2&#96;</span><br><span class="line">echo &quot;2-2&#x3D;$val&quot;</span><br><span class="line">val&#x3D;&#96;expr 2 \* 2&#96;                   # 乘号*必须转义</span><br><span class="line">echo &quot;2*2&#x3D;$val&quot;</span><br><span class="line">val&#x3D;&#96;expr 2 % 2&#96;</span><br><span class="line">echo &quot;2%2&#x3D;$val&quot;</span><br><span class="line"></span><br><span class="line">a&#x3D;10</span><br><span class="line">b&#x3D;20</span><br><span class="line"></span><br><span class="line">if [ $a &#x3D;&#x3D; $b]; then</span><br><span class="line">    echo &quot;a 等于 b&quot;</span><br><span class="line">fi</span><br><span class="line">if [ $a !&#x3D; $b]; then</span><br><span class="line">    echo &quot;a 不等于 b&quot;</span><br><span class="line">fi</span><br><span class="line">if [ $a -lt 100 -a $b -gt 15 ]; then</span><br><span class="line">    echo &quot;a 小于 100 且 b 大于 15,-a表示且,也可以使用&amp;&amp;&quot;</span><br><span class="line">fi</span><br><span class="line">if [ $a -lt 15 -o $b -lt 15 ]; then</span><br><span class="line">    echo &quot;a 小于 15 或 b 小于 15,-o表示或，也可以使用||&quot;</span><br><span class="line">fi</span><br><span class="line">if [ ! false ]; then</span><br><span class="line">    echo &quot;非运算为true&quot;</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">str&#x3D;&quot;welcome to shell&quot;</span><br><span class="line">if [ &quot;$str&quot; &#x3D; &quot;welcome to shell&quot; ]; then</span><br><span class="line">    echo &quot;&#x3D;号测试成功,&#x3D;&#x3D;号和&#x3D;号在[]中表示判断(字符串比较)时是等价的,!&#x3D;是测试字符串不等于&quot;</span><br><span class="line">fi</span><br><span class="line">if [ -z &quot;$str1&quot; ]; then</span><br><span class="line">    echo &quot;str1字符串不存在，所以使用-z返回true,最好用双引号括起来&quot;</span><br><span class="line">fi</span><br><span class="line">if [ -n &quot;$str&quot; ]; then</span><br><span class="line">    echo &quot;str字符串已存在，长度大于0，-n返回true,最好用双引号括起来&quot;</span><br><span class="line">fi</span><br><span class="line">if [ &quot;$str&quot; ]; then</span><br><span class="line">    echo &quot;使用$来检查字符串是否为空,如果字符串里面有空格必须用双引号括起来&quot;</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"># 文件测试运算符</span><br><span class="line">[ -b &#x2F;dev&#x2F;sda ] &amp;&amp; echo &quot;is Block Device Driver 块设备文件&quot;</span><br><span class="line">[ -c &#x2F;dev&#x2F;tty7 ] &amp;&amp; echo &quot;is Character Device Drive 字符设备文件&quot;</span><br><span class="line">[ -d &#x2F;dev&#x2F; ] &amp;&amp; echo &quot;is directory 目录&quot;</span><br><span class="line">[ -f &#x2F;etc&#x2F;nginx&#x2F;nginx.conf ] &amp;&amp; echo &quot;is file 文件&quot;</span><br><span class="line">[ -g &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is SUID&#x2F;SGID set group id&quot;</span><br><span class="line">[ -k &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is Sticky Bit&quot;</span><br><span class="line">[ -p &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 有名管道&quot;</span><br><span class="line">[ -u &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is SUID 位 set user id&quot;</span><br><span class="line">[ -r &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 可读&quot;</span><br><span class="line">[ -w &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 可写&quot;</span><br><span class="line">[ -x &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 可执行&quot;</span><br><span class="line">[ -s &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 不为空&quot;</span><br><span class="line">[ -e &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 文件或目录已存在&quot;</span><br><span class="line">[ -L &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is 符号链接&quot;</span><br><span class="line">[ -S &#x2F;etc&#x2F;passwd ] &amp;&amp; echo &quot;is socket&quot;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="命令实例"><a href="#命令实例" class="headerlink" title="命令实例"></a>命令实例</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;bin&#x2F;sh</span><br><span class="line">read name </span><br><span class="line">echo &quot;$name It is a test&quot;</span><br><span class="line">echo -e &quot;OK! \c&quot; # -e 开启转义 \c 不换行</span><br><span class="line">echo &quot;It is a test&quot;</span><br><span class="line">echo &#96;date&#96;</span><br><span class="line">printf &quot;Hello, Shell\n&quot;</span><br><span class="line"># %s %c %d %f都是格式替代符</span><br><span class="line"># %-10s 指一个宽度为10个字符（-表示左对齐，没有则表示右对齐），任何字符都会被显示在10个字符宽的字符内，如果不足则自动以空格填充，超过也会将内容全部显示出来。</span><br><span class="line"># %-4.2f 指格式化为小数，其中.2指保留2位小数。</span><br><span class="line">printf &quot;%-10s %-8s %-4s\n&quot; 姓名 性别 体重kg  </span><br><span class="line">printf &quot;%-10s %-8s %-4.2f\n&quot; 郭靖 男 66.1234 </span><br><span class="line">printf &quot;%-10s %-8s %-4.2f\n&quot; 杨过 男 48.6543 </span><br><span class="line">printf &quot;%-10s %-8s %-4.2f\n&quot; 郭芙 女 47.9876  </span><br><span class="line"></span><br><span class="line">printf &quot;%s\n&quot; abc def</span><br><span class="line"># 如果没有 arguments，那么 %s 用NULL代替，%d 用 0 代替</span><br><span class="line">printf &quot;%s and %d \n&quot; </span><br><span class="line"></span><br><span class="line">result&#x3D;$[a+b] # 注意等号两边不能有空格</span><br><span class="line">echo &quot;result 为： $result&quot;</span><br><span class="line"></span><br><span class="line">test 和 [] 等效</span><br><span class="line">if test -e .&#x2F;notFile -o -e .&#x2F;bash</span><br><span class="line">then</span><br><span class="line">    echo &quot;至少存在一个文件&quot;</span><br><span class="line">else</span><br><span class="line">    echo &quot;两个文件都不存在&quot;</span><br><span class="line">fi</span><br><span class="line">if [ -e .&#x2F;notFile -o -e .&#x2F;bash ]</span><br><span class="line">then</span><br><span class="line">    echo &quot;至少存在一个文件&quot;</span><br><span class="line">else</span><br><span class="line">    echo &quot;两个文件都不存在&quot;</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">a&#x3D;10</span><br><span class="line">b&#x3D;20</span><br><span class="line">if [ $a &#x3D;&#x3D; $b ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;a 等于 b&quot;</span><br><span class="line">elif [ $a -gt $b ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;a 大于 b&quot;</span><br><span class="line">elif [ $a -lt $b ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;a 小于 b&quot;</span><br><span class="line">else</span><br><span class="line">   echo &quot;没有符合的条件&quot;</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">for loop in 1 2 3 4 5</span><br><span class="line">do</span><br><span class="line">    echo &quot;The value is: $loop&quot;</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">int&#x3D;1</span><br><span class="line">while(( $int&lt;&#x3D;5 ))</span><br><span class="line">do</span><br><span class="line">    echo $int</span><br><span class="line">    let &quot;int++&quot;  #  let 命令，它用于执行一个或多个表达式，变量计算中不需要加上 $ 来表示变量</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"># 无限循环</span><br><span class="line">while :</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">    command</span><br><span class="line">done</span><br><span class="line">for (( ; ; ))</span><br><span class="line"></span><br><span class="line">a&#x3D;0</span><br><span class="line"></span><br><span class="line">until [ ! $a -lt 10 ]</span><br><span class="line">do</span><br><span class="line">   echo $a</span><br><span class="line">   a&#x3D;&#96;expr $a + 1&#96;</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">echo &#39;输入 1 到 4 之间的数字:&#39;</span><br><span class="line">echo &#39;你输入的数字为:&#39;</span><br><span class="line">read aNum</span><br><span class="line">case $aNum in</span><br><span class="line">    1)  echo &#39;你选择了 1&#39;</span><br><span class="line">    ;;</span><br><span class="line">    2)  echo &#39;你选择了 2&#39;</span><br><span class="line">    ;;</span><br><span class="line">    3)  echo &#39;你选择了 3&#39;</span><br><span class="line">    ;;</span><br><span class="line">    4)  echo &#39;你选择了 4&#39;</span><br><span class="line">        break</span><br><span class="line">    ;;</span><br><span class="line">    *)  echo &#39;你没有输入 1 到 4 之间的数字&#39;</span><br><span class="line">    ;;</span><br><span class="line">esac</span><br><span class="line"></span><br><span class="line">while :</span><br><span class="line">do</span><br><span class="line">    echo -n &quot;输入 1 到 5 之间的数字:&quot;</span><br><span class="line">    read aNum</span><br><span class="line">    case $aNum in</span><br><span class="line">        1|2|3|4|5) echo &quot;你输入的数字为 $aNum!&quot;</span><br><span class="line">        ;;</span><br><span class="line">        *) echo &quot;你输入的数字不是 1 到 5 之间的! 游戏结束&quot;</span><br><span class="line">            continue  # 永远不会结束</span><br><span class="line">            break     # 保留break跳出循环</span><br><span class="line">        ;;</span><br><span class="line">    esac</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"># 函数</span><br><span class="line">function demoFun1()&#123;</span><br><span class="line">    echo &quot;这是我的第一个 shell 函数!&quot;</span><br><span class="line">    return &#96;expr 1 + 1&#96;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">demoFun1</span><br><span class="line">echo $?</span><br><span class="line"></span><br><span class="line">init()&#123; :; &#125;  # 空函数</span><br></pre></td></tr></table></figure>

<h3 id="输入-输出重定向"><a href="#输入-输出重定向" class="headerlink" title="输入/输出重定向"></a>输入/输出重定向</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wc -l &lt;&lt; EOF</span><br><span class="line">    欢迎来到</span><br><span class="line">    菜鸟教程</span><br><span class="line">    www.runoob.com</span><br><span class="line">    dddddd</span><br><span class="line">EOF</span><br><span class="line"></span><br><span class="line"># 0 是标准输入（STDIN），1 是标准输出（STDOUT），2 是标准错误输出（STDERR）。</span><br><span class="line"># 如果希望将 stdout 和 stderr 合并后重定向到 file，可以这样写</span><br><span class="line">command &gt; file 2&gt;&amp;1   # 替换</span><br><span class="line">command &gt;&gt; file 2&gt;&amp;1  # 追加</span><br><span class="line">command &gt; &#x2F;dev&#x2F;null 2&gt;&amp;1  # 直接丢弃结果</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="文件包含"><a href="#文件包含" class="headerlink" title="文件包含"></a>文件包含</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">. filename   # 注意点号(.)和文件名中间有一空格</span><br><span class="line">source filename</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="linux-命令"><a href="#linux-命令" class="headerlink" title="linux 命令"></a>linux 命令</h3><h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><ul>
<li>遍历文件相加</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">echo -e &quot;1\n2\n3\n4&quot; &gt; test.txt</span><br><span class="line">awk &#39;&#123; sum +&#x3D; $1 &#125;; END &#123; print sum &#125;&#39; test.txt</span><br><span class="line">10</span><br></pre></td></tr></table></figure>

<ul>
<li>按：分割，打印所有用户名</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">awk -F: &#39;&#123; print $1 &#125;&#39; &#x2F;etc&#x2F;passwd</span><br></pre></td></tr></table></figure>

<ul>
<li>读取配置文件,并输出到变量里</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">echo -e &quot;DB_PWD&#x3D;123456\nDB_USER&#x3D;root&quot; &gt; test.txt</span><br><span class="line">cat test.txt |sed &quot;s#&#x2F;&#x2F;# #g&quot; | sed &quot;s&#x2F;&#x3D;&#x2F; &#x2F;g&quot; | awk  &#39;&#123;if(NF&gt;1)&#123;printf(&quot;%s&#x3D;\&quot;%s\&quot;;&quot;,$1,$2)&#125;&#125;&#39;</span><br><span class="line">DB_PWD&#x3D;&quot;123456&quot;;DB_USER&#x3D;&quot;root&quot;;</span><br><span class="line">eval $(cat test.txt |sed &quot;s#&#x2F;&#x2F;# #g&quot; | sed &quot;s&#x2F;&#x3D;&#x2F; &#x2F;g&quot; | awk  &#39;&#123;if(NF&gt;1)&#123;printf(&quot;%s&#x3D;\&quot;%s\&quot;;&quot;,$1,$2)&#125;&#125;&#39;)</span><br><span class="line">echo $DB_PWD</span><br><span class="line">123456</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>分析url</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">param&#x3D;&#96;echo &quot;$redirectUrl&quot; | awk -F &#39;?&#39; &#39;&#123;print $2&#125;&#39;&#96;;</span><br><span class="line">echo &quot;$param&quot; | awk -F &#39;&amp;&#39; &#39;&#123;i&#x3D;1;while(i&lt;&#x3D;NF)&#123;n&#x3D;split($i,array,&quot;&#x3D;&quot;);if(array[1]&#x3D;&#x3D;&quot;code&quot;)&#123;print array[2];break;&#125;;i++;&#125;&#125;&#39;</span><br></pre></td></tr></table></figure>

<ul>
<li>分析php配置文件，找出session节host配置项</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">awk -F &#39;&#x3D;&gt;&#39; &#39;&#123;if($0~&#x2F;\047session\047&#x2F;)&#123;find&#x3D;1;fl&#x3D;0;fr&#x3D;0&#125;if(find)&#123;if($0~&#x2F;\[&#x2F;)&#123;fl++;&#125;;if($0~&#x2F;\]&#x2F;)&#123;fr++;&#125;;if($1~&#x2F;\047host\047&#x2F;)&#123;print $2;exit 0;&#125;if(fl&#x3D;&#x3D;fr)&#123;exit 99&#125;&#125;&#125;&#39; ..&#x2F;application&#x2F;config.php | awk -F &#39;\047&#39; &#39;&#123;print $2,exit 0&#125;&#39;</span><br><span class="line">&#39;127.0.0.1&#39;,</span><br></pre></td></tr></table></figure>

<h4 id="curl"><a href="#curl" class="headerlink" title="curl"></a>curl</h4><ul>
<li>静默方式-s，-k不检查证实 -c 创建cookie文件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">result&#x3D;&#96;curl -s -k -c cxl_cookie https:&#x2F;&#x2F;10.0.0.42:1066&#96;</span><br><span class="line"> echo $result</span><br><span class="line">&#123;&quot;status&quot;:0,&quot;info&quot;:&quot;调用接口成功&quot;,&quot;data&quot;:&quot;home&quot;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>通过使用 -v 和 -trace获取更多的链接信息</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl  -v  -k -c &quot;&quot; -d &quot;&quot; -H &quot;&quot; https:&#x2F;&#x2F;10.0.0.42:1066</span><br></pre></td></tr></table></figure>

<ul>
<li>打印返回的header（-i）</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">curl  -i  -k -c &quot;&quot; -d &quot;&quot; -H &quot;&quot; https:&#x2F;&#x2F;10.0.0.42:1066</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="cut"><a href="#cut" class="headerlink" title="cut"></a>cut</h4><ul>
<li>按字节剪切</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;test&quot; | cut --b 2-4</span><br><span class="line">est</span><br></pre></td></tr></table></figure>

<ul>
<li>按字符剪切</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;test&quot; | cut -c 2-4</span><br><span class="line">est</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>多个分段用逗号分开</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;test test&quot;|cut -b 3-5,8</span><br><span class="line">st s</span><br></pre></td></tr></table></figure>

<ul>
<li>从文件获取</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">echo -e &quot;星期一\n星期二\n星期三&quot; &gt; test.txt</span><br><span class="line">cut -b 1-3 test.txt</span><br><span class="line">星</span><br><span class="line">星</span><br><span class="line">星</span><br></pre></td></tr></table></figure>

<ul>
<li>中文</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;星期四 星期三&quot; | cut -c 1-3</span><br><span class="line">星</span><br></pre></td></tr></table></figure>

<ul>
<li>按特殊字符分割</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;root:test:ok&quot; | cut -d : -f 1</span><br><span class="line">root</span><br></pre></td></tr></table></figure>

<ul>
<li>综合应用</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;root:test:ok&quot; | cut -d : -f 3 | cut -c 1</span><br><span class="line">o</span><br></pre></td></tr></table></figure>

<h4 id="sed"><a href="#sed" class="headerlink" title="sed"></a>sed</h4><ul>
<li>替换字符</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;this ReplaceWord success&quot; | sed &quot;s&#x2F;ReplaceWord&#x2F;is&#x2F;&quot;</span><br><span class="line">this is success</span><br></pre></td></tr></table></figure>

<ul>
<li>替换文件</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;this ReplaceWord success&quot; &gt; test.txt</span><br><span class="line">cat test.txt</span><br><span class="line">sed -i &quot;s&#x2F;ReplaceWord&#x2F;is&#x2F;&quot; test.txt</span><br><span class="line">cat test.txt</span><br></pre></td></tr></table></figure>

<ul>
<li>使用正则</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;this 1234 success&quot; | sed -r &quot;s&#x2F;[0-9]+&#x2F;is&#x2F;&quot;</span><br><span class="line">this is success</span><br></pre></td></tr></table></figure>

<ul>
<li>-g表示所有匹配的都执行</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;this 1234 success 1234&quot; | sed -r &quot;s&#x2F;[0-9]+&#x2F;is&#x2F;g&quot;</span><br><span class="line">this is success is</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li>按行号删除文件内容</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">sed -i &#39;1,50000d&#39; &quot;$cxl_log_file&quot; # 删除50000万行</span><br><span class="line">sed -i &#39;1d&#39; a.txt删首行</span><br><span class="line">sed -i &#39;$d&#39; b.txt删尾行</span><br><span class="line">sed -i &#39;s&#x2F;[ ]*&#x2F;&#x2F;g&#39; c.txt删空格</span><br><span class="line">sed -i &#39;&#x2F;^$&#x2F;d&#39; d.txt删空行</span><br><span class="line">sed -i ‘&#x2F;^[0-9]*$&#x2F;d&#39; a.txt删包含数字的行</span><br><span class="line">sed -i ‘1,2d’a.txt删2行</span><br><span class="line">sed -i ‘&#x2F;love&#x2F;d’ a.txt删包含string的行</span><br></pre></td></tr></table></figure>
<ul>
<li>修改指定行内容</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed -r &#39;s&#x2F;( &#39;host&#39; *).&#x2F;1&quot;test&quot;&#x2F;&#39; ..&#x2F;application&#x2F;config.php</span><br></pre></td></tr></table></figure>


<h4 id="tr"><a href="#tr" class="headerlink" title="tr"></a>tr</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#  转换为大写</span><br><span class="line">echo &quot;hello world&quot; | tr [:lower:] [:upper:]</span><br><span class="line"># 删除空格、数字和-号</span><br><span class="line">echo &quot;hello-123-world  empty&quot; | tr -d &#39;[:blank:][:digit:]-&#39;</span><br><span class="line"># 删除除空格、数字和-号以外的字符，和上一例子正相反</span><br><span class="line">echo &quot;hello - 123-world  empty&quot; | tr -d -c &#39;[:blank:][:digit:]-&#39;</span><br><span class="line"># 去掉连续重复字符</span><br><span class="line">echo &quot;helloooo oooo    isssso ookk&quot; | tr -s &quot; os&quot;</span><br><span class="line"># 删除window文件造成^M字符</span><br><span class="line">cat file | tr -s &quot;\r&quot; &quot;\n&quot; &gt; new_file</span><br><span class="line">cat file | tr -d &quot;\r&quot; &gt; new_file</span><br></pre></td></tr></table></figure>

<h4 id="uname"><a href="#uname" class="headerlink" title="uname"></a>uname</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"># 打印全部</span><br><span class="line">uname -a</span><br><span class="line">Linux 1604developer 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x86_64 x86_64 GNU&#x2F;Linux</span><br><span class="line"># 打印kernel-name</span><br><span class="line">uname -s</span><br><span class="line">Linux</span><br><span class="line"># 打印机器网络名</span><br><span class="line">uname -n</span><br><span class="line">1604developer</span><br><span class="line"># 打印kernel-release</span><br><span class="line">uname -r</span><br><span class="line">4.4.0-89-generic</span><br><span class="line"># 打印kernel-version</span><br><span class="line">uname -v</span><br><span class="line">#112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017</span><br><span class="line"># 打印机器硬件名称</span><br><span class="line">uname -m</span><br><span class="line">x86_64</span><br><span class="line"># 打印处理器类型</span><br><span class="line">uname -p</span><br><span class="line">x86_64</span><br><span class="line"># 打印硬件平台</span><br><span class="line">uname -i</span><br><span class="line">x86_64</span><br><span class="line"># 打印操作系统</span><br><span class="line">uname -o</span><br><span class="line">GNU&#x2F;Linux</span><br></pre></td></tr></table></figure>

<h4 id="mktemp"><a href="#mktemp" class="headerlink" title="mktemp"></a>mktemp</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 生成随机文件</span><br><span class="line">mktemp ttXXXX.tmp</span><br><span class="line"># 生成随机目录</span><br><span class="line">mktemp -d tempXXXXXX</span><br></pre></td></tr></table></figure>

<h4 id="jq"><a href="#jq" class="headerlink" title="jq"></a>jq</h4><p>jq不是标准命令，是用来解析json字符串的命令，使用apt install jq -y 来安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"># get value</span><br><span class="line">echo &#39;&#123;&quot;status&quot;:0,&quot;info&quot;:&quot;we are success&quot;,&quot;data&quot;:&#123;&quot;client&quot;:&#123;&quot;client_id&quot;:&quot;123456&quot;,&quot;client_secret&quot;:&quot;567890&quot;&#125;&#125;&#125;&#39; | jq &quot;.status&quot;</span><br><span class="line">0</span><br><span class="line">echo &#39;&#123;&quot;status&quot;:0,&quot;info&quot;:&quot;we are success&quot;,&quot;data&quot;:&#123;&quot;client&quot;:&#123;&quot;client_id&quot;:&quot;123456&quot;,&quot;client_secret&quot;:&quot;567890&quot;&#125;&#125;&#125;&#39; | jq &quot;.info&quot; | sed &#39;s&#x2F;&quot;&#x2F;&#x2F;g&#39;</span><br><span class="line">we are success</span><br><span class="line">echo &#39;&#123;&quot;status&quot;:0,&quot;info&quot;:&quot;we are success&quot;,&quot;data&quot;:&#123;&quot;client&quot;:&#123;&quot;client_id&quot;:&quot;123456&quot;,&quot;client_secret&quot;:&quot;567890&quot;&#125;&#125;&#125;&#39; | jq &quot;.data.client.client_id&quot; | sed &#39;s&#x2F;&quot;&#x2F;&#x2F;g&#39;</span><br><span class="line">123456</span><br><span class="line"># filter data</span><br><span class="line">echo &#39;&#123;&quot;status&quot;:0,&quot;info&quot;:&quot;we are success&quot;,&quot;data&quot;:&#123;&quot;client&quot;:&#123;&quot;client_id&quot;:&quot;123456&quot;,&quot;client_secret&quot;:&quot;567890&quot;&#125;&#125;&#125;&#39; | jq &#39;.data|.client&#39;</span><br><span class="line">&#123;</span><br><span class="line">  &quot;client_id&quot;: &quot;123456&quot;,</span><br><span class="line">  &quot;client_secret&quot;: &quot;567890&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="xargs"><a href="#xargs" class="headerlink" title="xargs"></a>xargs</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 当你尝试用rm 删除太多的文件，你可能得到一个错误信息：&#x2F;bin&#x2F;rm Argument list too long. 用xargs 去避免这个问题</span><br><span class="line">find ~ -name &#39;*.log&#39; -print0 | xargs -0 rm -f</span><br><span class="line"># 获得&#x2F;etc&#x2F; 下所有*.conf 结尾的文件列表，有几种不同的方法能得到相同的结果，下面的例子仅仅是示范怎么实用xargs ，在这个例子中实用 xargs将find 命令的输出传递给ls -l</span><br><span class="line">find &#x2F;etc -name &quot;*.conf&quot; | xargs ls –l</span><br><span class="line"># 假如你有一个文件包含了很多你希望下载的URL, 你能够使用xargs 下载所有链接</span><br><span class="line">cat url-list.txt | xargs wget –c</span><br><span class="line"># 查找所有的jpg 文件，并且压缩它</span><br><span class="line">find &#x2F; -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz</span><br><span class="line"># 拷贝所有的图片文件到一个外部的硬盘驱动</span><br><span class="line">ls *.jpg | xargs -n1 -i cp &#123;&#125; &#x2F;external-hard-drive&#x2F;directory</span><br><span class="line"># 查找另一个目录同名文件，-n1表示每行一个参数 -i表示用&#123;&#125;表示输出的每行记录</span><br><span class="line">ls &#x2F;home&#x2F;cxl&#x2F;git-svn&#x2F;spi&#x2F;spi-php&#x2F;bin&#x2F;res&#x2F;supervisor | xargs -n1 -i echo &#123;&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/shell/" rel="tag"><i class="fa fa-tag"></i> shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/26/shell-shunit2/" rel="prev" title="shell-shunit2">
      <i class="fa fa-chevron-left"></i> shell-shunit2
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/09/16/linux-openssl/" rel="next" title="linux-openssl">
      linux-openssl <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    
  <div class="comments">
    <div id="SOHUCS"></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">京ICP备 测试1234567890号-1 </a>
      <img src="http://www.beian.gov.cn/img/new/gongan.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=%E6%B5%8B%E8%AF%951234567890" rel="noopener" target="_blank">京公网安备 测试1234567890号 </a>
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">刀客林</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">41k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">37 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.0/lib/anime.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>











<script>
document.querySelectorAll('.pdfobject-container').forEach(element => {
  let url = element.dataset.target;
  let pdfOpenParams = {
    navpanes : 0,
    toolbar  : 0,
    statusbar: 0,
    pagemode : 'thumbs',
    view     : 'FitH'
  };
  let pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
  let fullURL = `/lib/pdf/web/viewer.html?file=${encodeURIComponent(url)}${pdfOpenFragment}`;

  if (NexT.utils.supportsPDFs()) {
    element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
  } else {
    element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
  }
});
</script>


<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8.6.4/dist/mermaid.min.js', () => {
    mermaid.init({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    }, '.mermaid');
  }, window.mermaid);
}
</script>


  

  

  <script>
  NexT.utils.loadComments('#SOHUCS', () => {
    NexT.utils.getScript('https://changyan.sohu.com/upload/changyan.js', () => {
      window.changyan.api.config({
        appid: 'cyuYT0YXi',
        conf : '84caa3b92fd5f3996f4e117aad400242'
      });
    });
  });
  </script>
  <script src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>

</body>
</html>
